home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uemlsrc.arc / basic.c next >
C/C++ Source or Header  |  1987-08-24  |  9KB  |  328 lines

  1. /*
  2.  * The routines in this file
  3.  * move the cursor around on the screen.
  4.  * They compute a new value for the cursor, then
  5.  * adjust ".". The display code always updates the
  6.  * cursor location, so only moves between lines,
  7.  * or functions that adjust the top line in the window
  8.  * and invalidate the framing, are hard.
  9.  */
  10. #include        <stdio.h>
  11. #include        "ed.h"
  12.  
  13. /*
  14.  * Move the cursor to the
  15.  * beginning of the current line
  16.  * or the beginning of the last
  17.  * line if already at beginning.
  18.  * Trivial.
  19.  */
  20. gotobol(f, n)
  21. register int f, n;
  22. {
  23.         if(curwp->w_doto == 0)
  24.                 backline(f, n);
  25.         curwp->w_doto  = 0;
  26.         return (TRUE);
  27. }
  28.  
  29. /*
  30.  * Move the cursor backwards by
  31.  * "n" characters. If "n" is less than
  32.  * zero call "forwchar" to actually do the
  33.  * move. Otherwise compute the new cursor
  34.  * location. Error if you try and move
  35.  * out of the buffer. Set the flag if the
  36.  * line pointer for dot changes.
  37.  */
  38. backchar(f, n)
  39. register int    f, n;
  40. {
  41.         register LINE   *lp;
  42.  
  43.         if (n < 0)
  44.                 return (forwchar(f, -n));
  45.         while (n--) {
  46.                 if (curwp->w_doto == 0) {
  47.                         if ((lp=lback(curwp->w_dotp)) == curbp->b_linep)
  48.                                 return (FALSE);
  49.                         curwp->w_dotp  = lp;
  50.                         curwp->w_doto  = llength(lp);
  51.                         curwp->w_flag |= WFMOVE;
  52.                 } else
  53.                         curwp->w_doto--;
  54.         }
  55.         return (TRUE);
  56. }
  57.  
  58. /*
  59.  * Move the cursor to the end
  60.  * of the current line or the
  61.  * end of the next line if
  62.  * dot is already at the end.
  63.  * Trivial.  No errors.
  64.  */
  65. gotoeol(f, n)
  66. register int f, n;
  67. {
  68.         if (curwp->w_doto == llength(curwp->w_dotp))
  69.                 forwline(f, n);
  70.         curwp->w_doto = llength(curwp->w_dotp);
  71.         return (TRUE);
  72. }
  73.  
  74. /*
  75.  * Move the cursor forwwards by
  76.  * "n" characters. If "n" is less than
  77.  * zero call "backchar" to actually do the
  78.  * move. Otherwise compute the new cursor
  79.  * location, and move ".". Error if you
  80.  * try and move off the end of the
  81.  * buffer. Set the flag if the line pointer
  82.  * for dot changes.
  83.  */
  84. forwchar(f, n)
  85. register int    f, n;
  86. {
  87.         if (n < 0)
  88.                 return (backchar(f, -n));
  89.         while (n--) {
  90.                 if (curwp->w_doto == llength(curwp->w_dotp)) {
  91.                         if (curwp->w_dotp == curbp->b_linep)
  92.                                 return (FALSE);
  93.                         curwp->w_dotp  = lforw(curwp->w_dotp);
  94.                         curwp->w_doto  = 0;
  95.                         curwp->w_flag |= WFMOVE;
  96.                 } else
  97.                         curwp->w_doto++;
  98.         }
  99.         return (TRUE);
  100. }
  101.  
  102. /*
  103.  * Goto the beginning of the buffer.
  104.  * Massive adjustment of dot. This is considered
  105.  * to be hard motion; it really isn't if the original
  106.  * value of dot is the same as the new value of dot.
  107.  * Normally bound to "M-<".
  108.  */
  109. gotobob(f, n)
  110. register int f, n;
  111. {
  112.         curwp->w_dotp  = lforw(curbp->b_linep);
  113.         curwp->w_doto  = 0;
  114.         curwp->w_flag |= WFHARD;
  115.         return (TRUE);
  116. }
  117.  
  118. /*
  119.  * Move to the end of the buffer.
  120.  * Dot is always put at the end of the
  121.  * file (ZJ). The standard screen code does
  122.  * most of the hard parts of update. Bound to
  123.  * "M->".
  124.  */
  125. gotoeob(f, n)
  126. register int f, n;
  127. {
  128.         curwp->w_dotp  = curbp->b_linep;
  129.         curwp->w_doto  = 0;
  130.         curwp->w_flag |= WFHARD;
  131.         return (TRUE);
  132. }
  133.  
  134. /*
  135.  * Move forward by full lines.
  136.  * If the number of lines to move is less
  137.  * than zero, call the backward line function to
  138.  * actually do it. The last command controls how
  139.  * the goal column is set. Bound to "C-N". No
  140.  * errors are possible.
  141.  */
  142. forwline(f, n)
  143. register int f, n;
  144. {
  145.         register LINE   *dlp;
  146.  
  147.         if (n < 0)
  148.                 return (backline(f, -n));
  149.         if ((lastflag&CFCPCN) == 0)             /* Reset goal if last   */
  150.                 curgoal = curcol;               /* not C-P or C-N       */
  151.         thisflag |= CFCPCN;
  152.         dlp = curwp->w_dotp;
  153.         while (n-- && dlp!=curbp->b_linep)
  154.                 dlp = lforw(dlp);
  155.         curwp->w_dotp  = dlp;
  156.         curwp->w_doto  = getgoal(dlp);
  157.         curwp->w_flag |= WFMOVE;
  158.         return (TRUE);
  159. }
  160.  
  161. /*
  162.  * This function is like "forwline", but
  163.  * goes backwards. The scheme is exactly the same.
  164.  * Check for arguments that are less than zero and
  165.  * call your alternate. Figure out the new line and
  166.  * call "movedot" to perform the motion. No errors
  167.  * are possible. Bound to "C-P".
  168.  */
  169. backline(f, n)
  170. register int f, n;
  171. {
  172.         register LINE   *dlp;
  173.  
  174.         if (n < 0)
  175.                 return (forwline(f, -n));
  176.         if ((lastflag&CFCPCN) == 0)             /* Reset goal if the    */
  177.                 curgoal = curcol;               /* last isn't C-P, C-N  */
  178.         thisflag |= CFCPCN;
  179.         dlp = curwp->w_dotp;
  180.         while (n-- && lback(dlp)!=curbp->b_linep)
  181.                 dlp = lback(dlp);
  182.         curwp->w_dotp  = dlp;
  183.         curwp->w_doto  = getgoal(dlp);
  184.         curwp->w_flag |= WFMOVE;
  185.         return (TRUE);
  186. }
  187.  
  188. /*
  189.  * This routine, given a pointer to
  190.  * a LINE, and the current cursor goal
  191.  * column, return the best choice for the
  192.  * offset. The offset is returned.
  193.  * Used by "C-N" and "C-P".
  194.  */
  195. getgoal(dlp)
  196. register LINE   *dlp;
  197. {
  198.         register int    c;
  199.         register int    col;
  200.         register int    newcol;
  201.         register int    dbo;
  202.  
  203.         col = 0;
  204.         dbo = 0;
  205.         while (dbo != llength(dlp)) {
  206.                 c = lgetc(dlp, dbo);
  207.                 newcol = col;
  208.                 if (c == '\t')
  209.                         newcol |= 0x07;
  210.                 else if (c<0x20 || c==0x7F)
  211.                         ++newcol;
  212.                 ++newcol;
  213.                 if (newcol > curgoal)
  214.                         break;
  215.                 col = newcol;
  216.                 ++dbo;
  217.         }
  218.         return (dbo);
  219. }
  220.  
  221. /*
  222.  * Scroll forward by a specified number
  223.  * of lines, or by a full page if no argument.
  224.  * Bound to "C-V". The "2" in the arithmetic on
  225.  * the window size is the overlap; this value is
  226.  * the default overlap value in ITS EMACS.
  227.  * Because this zaps the top line in the display
  228.  * window, we have to do a hard update.
  229.  */
  230. forwpage(f, n)
  231. register int    f, n;
  232. {
  233.         register LINE   *lp;
  234.  
  235.         if (f == FALSE) {
  236.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  237.                 if (n <= 0)                     /* Forget the overlap   */
  238.                         n = 1;                  /* if tiny window.      */
  239.         } else if (n < 0)
  240.                 return (backpage(f, -n));
  241. #if     CVMVAS
  242.         else                                    /* Convert from pages   */
  243.                 n *= curwp->w_ntrows;           /* to lines.            */
  244. #endif
  245.         lp = curwp->w_linep;
  246.         while (n-- && lp!=curbp->b_linep)
  247.                 lp = lforw(lp);
  248.         curwp->w_linep = lp;
  249.         curwp->w_dotp  = lp;
  250.         curwp->w_doto  = 0;
  251.         curwp->w_flag |= WFHARD;
  252.         return (TRUE);
  253. }
  254.  
  255. /*
  256.  * This command is like "forwpage",
  257.  * but it goes backwards. The "2", like above,
  258.  * is the overlap between the two windows. The
  259.  * value is from the ITS EMACS manual. Bound
  260.  * to "M-V". We do a hard update for exactly
  261.  * the same reason.
  262.  */
  263. backpage(f, n)
  264. register int    f, n;
  265. {
  266.         register LINE   *lp;
  267.  
  268.         if (f == FALSE) {
  269.                 n = curwp->w_ntrows - 2;        /* Default scroll.      */
  270.                 if (n <= 0)                     /* Don't blow up if the */
  271.                         n = 1;                  /* window is tiny.      */
  272.         } else if (n < 0)
  273.                 return (forwpage(f, -n));
  274. #if     CVMVAS
  275.         else                                    /* Convert from pages   */
  276.                 n *= curwp->w_ntrows;           /* to lines.            */
  277. #endif
  278.         lp = curwp->w_linep;
  279.         while (n-- && lback(lp)!=curbp->b_linep)
  280.                 lp = lback(lp);
  281.         curwp->w_linep = lp;
  282.         curwp->w_dotp  = lp;
  283.         curwp->w_doto  = 0;
  284.         curwp->w_flag |= WFHARD;
  285.         return (TRUE);
  286. }
  287.  
  288. /*
  289.  * Set the mark in the current window
  290.  * to the value of "." in the window. No errors
  291.  * are possible.